library BlizzardAbility initializer Init requires entities, utils, SpellBar
    /*
    --------------------------------------------------------------------------
    # 暴风雪
    --------------------------------------------------------------------------
    */

    globals
        public integer ABILITY_ID = 'AHbz'
    endglobals

    private function anon_ed2729f5bb11 takes nothing returns nothing
        local Spell spell = ReleaseTimer(GetExpiredTimer())

        if not spell.m_isSpelling then
            call spell.destroy()
        endif
    endfunction
    
    private function Release takes Spell spell returns nothing
        call SpellBar.get(spell.m_unit).destroy()
        call TimerStart(NewTimerEx(spell), 1.0, false, function anon_ed2729f5bb11)
    endfunction

    private function SpellEndCast takes nothing returns nothing
        local Spell spell = Spell.get(SpellEvent.CastingUnit, ABILITY_ID)

        set spell.m_isSpelling = false

        call Release(spell)
    endfunction

    private function SpellAction takes Spell spell returns nothing
        local real x = spell.m_targetX
        local real y = spell.m_targetY
        local real range = spell.m_rangeEffect
        local UnitGroup gd
        local Link link
        local Unit ufd

        set gd = UnitGroup.fromArea(x, y, range)

        set link = gd.first
        loop
            exitwhen (link <= 0)
            set ufd = Unit(link.data)

            if IsWidgetAlive(ufd.m_unit) /* 存活的
            */then
                call spell.damageUnit(ufd.m_unit)
            endif
            set link = link.next
        endloop

        call gd.destroy()
    endfunction

    private function anon_b987db548e5b takes nothing returns nothing
        local Spell spell = ReleaseTimer(GetExpiredTimer())
        call SpellAction(spell)
    endfunction
    private function anon_edd34f52af7a takes nothing returns nothing
        local Spell spell = GetTimerData(GetExpiredTimer())
        call TimerStart(NewTimerEx(spell), 0.2, false, function anon_b987db548e5b)
    endfunction

    private function SpellChannel takes nothing returns nothing
        local Spell spell
        local SpellBar spellBar
        local integer level

        if not (IsWidgetAlive(SpellEvent.CastingUnit)) then
            return
        endif

        set spell = Spell.create(SpellEvent.CastingUnit, ABILITY_ID)
        set spell.m_isSpelling = true

        set level = GetUnitAbilityLevel(spell.m_unit, ABILITY_ID)
        
        if level == 1 then
            //set spell.m_count = 6 //波次数量
            //set spell.m_int1 = 6 //碎片数量
            set spell.m_amount = 30 //每波伤害
            //set spell.m_real = 150 //每波最大伤害
            set spell.m_timeCast = 6.0
        elseif level == 2 then
            set spell.m_amount = 40 //每波伤害
            set spell.m_timeCast = 8.0
        elseif level == 3 then
            set spell.m_amount = 50 //每波伤害
            set spell.m_timeCast = 10.0
        endif

        set spell.m_structureAmountRate = 0.5 //建筑伤害参数
        set spell.m_rangeEffect = 200 //影响范围
        set spell.m_interval = 1.0 //波次间隔
        set spell.m_ignoreAlliance = true //不分敌我

        set spell.m_targetX = SpellEvent.TargetX
        set spell.m_targetY = SpellEvent.TargetY

        set spell.m_attackType = ATTACK_TYPE_MAGIC // 魔法
        set spell.m_damageType = DAMAGE_TYPE_COLD //普通(计算护甲) DAMAGE_TYPE_UNKNOWN //未知
        set spell.m_damageEffPath = null

        call TimerStart(spell.getTimer(), spell.m_interval, true, function anon_edd34f52af7a)

        set spellBar = SpellBar.create(spell.m_unit)
        set spellBar.m_isReverse = true //反向
        call spellBar.start(spell.m_timeCast, -60.0)
    endfunction

    private function Init takes nothing returns nothing
        call RegisterSpellChannelResponse(ABILITY_ID, SpellChannel)
        //call RegisterSpellEffectResponse(ABILITY_ID, SpellEffect)

        call RegisterSpellEndCastResponse(ABILITY_ID, SpellEndCast)
        //call RegisterSpellFinishResponse(ABILITY_ID, SpellFinish)
    endfunction

endlibrary
